<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" >
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordquery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employee" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="100" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==1">启用</span>
          <span style="color: red" v-else="scope.row.state==0">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" label="部门名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="shop_id" label="员工类型" width="100" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.shop_id==null">员工管理员</span>
          <span style="color: red" v-else >店铺管理员</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button type="primary" icon="el-icon-save" circle @click="handlesave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total,sizes, prev, pager, next,jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="sizes"
          :page-size="pageSize"
          :total="totals"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--添加修改-->
    <!--编辑界面-->
    <!--
      title="添加或修改" - 标题
      :visible.sync="saveFormVisible" - 控制当前对话框是否显示 - 默认为false不显示
      :close-on-click-modal="false" - 点击对话框以外的区域不关闭
    -->
    <el-dialog title="添加或修改" :visible.sync="saveFormVisible" :loading="saveLoading" :close-on-click-modal="false">
      <!--
        :model="saveForm"  - 表单绑定模型数据对象saveForm
        label-width="80px"  - 标签的宽度
        :rules="saveFormRules" - 验证规则 - 表单项校验
        ref="saveForm" - 为当前对话框取名saveForm - 做表单校验

        label="姓名" - 标签名字
        prop="name" - 要做校验的时候必须要，不做校验就没有必要
        v-model="saveForm.name" - 双向绑定
        auto-complete="off" - 自动完成 - 关闭
      -->
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="员工名称" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" >
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" >
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" >
          <el-input type="password" v-model="saveForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" >
          <el-input-number v-model="saveForm.age" auto-complete="off"></el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-select v-model="saveForm.department_id" placeholder="请选择">
            <el-option
                v-for="item in departments"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="saveForm.role_id" multiple @change="getRoleid" placeholder="请选择">
            <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺ID" >
          <el-input-number v-model="saveForm.shop_id" auto-complete="off"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {

  data() {
    return {
      //1.分页相关参数
      employee: [],     //绑定到表格 - 显示列表数据 - 请求完成之后
      totals: 0,          //总数量 - 计算分页栏 -  请求完成之
      currentPage: 1,     //当前页 - 分页栏默认当前页显示
      pageSize: 5,        //每页显示的条数  - 计算分页栏
      sizes:[5, 10, 20, 50],
      listLoading: false, //表格上的加载框

      //2.高级查询相关数据
      keyword: "",

      //3.批量删除相关参数
      sels: [],

      //4.添加或修改的相关数据
      saveFormVisible: false, //控制对话框是否显示 - 默认不显示
      saveLoading: false,     //保存的加载框 - 默认不显示
      saveFormRules: {        //表单项验证
        username: [
          {required: true, message: '请输入员工名称', trigger: 'blur'}
        ]
      },
      //5.添加和修改中对话框的数据
      saveForm: {
        id:null,
        username: null,
        phone: null,
        email: null,
        password:null,
        age: null,
        state: 1,
        department_id: null,
        parenshop_id: null,
        role_id:[],
      },

      //6.回显部门名称选择器相关数据
      departments: [],
      //7.角色数据
      roles:[]
    }
  },
  methods: {
    //1.每页条数
    handleSizeChange(val) {
      this.pageSize=val;
      this.getEmployee();
    },
    //2.当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmployee();
    },
    //3.高级查询
    keywordquery(){
      this.currentPage=1;
      this.getEmployee();
    },
    //1.获取用户列表
    getEmployee() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;

      this.$http.post("/employee",para).then(res=>{

        //给totals赋值 - 计算分页栏
        this.totals = res.data.totals;
        //赋值分页数据 - 显示当前页的分页数据
        this.employee = res.data.data;
        //隐藏加载框/忙等框
        this.listLoading = false;
      }).catch(res=> {
        this.$message.error('系统繁忙，请稍后重试!!!');
      })
    },
    //2.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/employee/"+row.id).then((res)=>{
          this.listLoading = false;
          if (res.data.success){
            this.$message.success("删除成功");
            if(((this.currentPage*this.pageSize)-this.totals)>=(this.pageSize-1)){
              this.currentPage=this.currentPage-1;
            }
            this.getEmployee();
          }else {
            this.$message.error("系统繁忙，请稍后再试！！！");
          }
        }).catch((res) => {
          this.$message.error("系统繁忙，请稍后再试！！！");
        });
      }).catch(()=>{

      })
    },
    //3.批量删除
    selsChange: function (sels) {
      this.sels = sels;
    },
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      console.log(ids);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/employee",ids).then((res) => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message.success("删除成功");
            this.currentPage =1;
            this.getEmployee();
          } else {
            this.$message.error("系统繁忙，请稍后再试  500！！！");
          }
        }).catch((res) => {
          this.$message.error("系统繁忙，请稍后再试 400！！！");
        });
      }).catch(() => {

      });
    },
    //4.显示编辑界面
    handlesave:function (index, row) {
      this.saveFormVisible = true;
      var rid=[];
      this.saveForm = Object.assign({}, row);
      row.role.forEach(e=>{
        rid.push(e.id)
      });
      this.getemployee();
      this.getRoles();
      this.saveForm.role_id=rid;
      console.log(this.saveForm.role_id)

    },
    //5.显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm  = {
        id:null,
        username: null,
        phone: null,
        email: null,
        password:null,
        age: null,
        state: 1,
        department_id: null,
        parenshop_id: null,
        role_id:[]
      };
      this.getemployee();
      this.getRoles();
    },
    getRoleid(val){
      console.log(val)
      //解决：el-select下拉框多选，选择后赋值成功，输入框不显示选中的值
      this.$nextTick(() => {
        this.$forceUpdate()
      })
    },
    //6.添加修改
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);

            this.$http.put("/employee", para).then((res) => {
              this.saveLoading = false;
              this.saveFormVisible = false;
              if (res.data.success) {
                this.$message.success("操作成功");
                this.getEmployee();
              }else {
                this.$message.error("系统繁忙，请稍后再试！！！");
              }
            }).catch((res)=>{
              this.$message.error("系统繁忙，请稍后再试！！！");
            });
          });
        }
      }).catch(()=>{

      });
    },
    //7.获取  部门名称数据
    getemployee(){
      this.$http.get("/department").then((res)=>{
        this.departments=res.data;
      })
    },
    //8.获取  角色数据
    getRoles(){
      this.$http.get("/role").then((res)=>{
        this.roles=res.data;
      })
    }
  },
  mounted() {
    this.getEmployee();
  }
}
</script>

<style scoped>

</style>